package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.ClassInfo;
import cn.stylefeng.guns.modular.school.entity.ClassTeacherLession;
import cn.stylefeng.guns.modular.school.mapper.ClassInfoMapper;
import cn.stylefeng.guns.modular.school.model.params.ClassInfoParam;
import cn.stylefeng.guns.modular.school.model.params.ClassTeacherLessionParam;
import cn.stylefeng.guns.modular.school.model.result.ClassInfoResult;
import cn.stylefeng.guns.modular.school.model.result.ClassTeacherLessionResult;
import cn.stylefeng.guns.modular.school.service.ClassInfoService;
import cn.stylefeng.guns.modular.school.service.ClassTeacherLessionService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 班级信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-11-25
 */
@Service
public class ClassInfoServiceImpl extends ServiceImpl<ClassInfoMapper, ClassInfo> implements ClassInfoService {
    @Autowired
    private ClassTeacherLessionService classTeacherLessionService;

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void add(ClassInfoParam param) {
        ClassInfo entity = getEntity(param);
        this.save(entity);

        ClassTeacherLession classTeacherLession = new ClassTeacherLession();
        classTeacherLession.setClassId(entity.getId());
        classTeacherLession.setTeacherId(param.getTeacherId());
        classTeacherLessionService.save(classTeacherLession);
    }

    @Override
    public void delete(ClassInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void update(ClassInfoParam param) {
        ClassInfo oldEntity = getOldEntity(param);
        Integer teacherId = oldEntity.getTeacherId();
        ClassInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);

        if (teacherId != newEntity.getTeacherId()) {
            ClassTeacherLessionParam classTeacherLessionParam = new ClassTeacherLessionParam();
            classTeacherLessionParam.setTeacherId(teacherId);
            classTeacherLessionParam.setClassId(newEntity.getId());
            ClassTeacherLessionResult classTeacherLessionResult = classTeacherLessionService.findBySpec(classTeacherLessionParam);
            if (classTeacherLessionResult != null) {
                classTeacherLessionService.removeById(classTeacherLessionResult.getId());
            }

            // 判断新班主任是否已经存在【classTeacherLession】中
            classTeacherLessionParam = new ClassTeacherLessionParam();
            classTeacherLessionParam.setTeacherId(param.getTeacherId());
            classTeacherLessionParam.setClassId(newEntity.getId());
            classTeacherLessionResult = classTeacherLessionService.findBySpec(classTeacherLessionParam);
            if (classTeacherLessionResult == null) {
                ClassTeacherLession classTeacherLession = new ClassTeacherLession();
                classTeacherLession.setClassId(newEntity.getId());
                classTeacherLession.setTeacherId(param.getTeacherId());
                classTeacherLessionService.save(classTeacherLession);
            }
        }
    }

    @Override
    public ClassInfoResult findBySpec(ClassInfoParam param) {
        return null;
    }

    @Override
    public List<ClassInfoResult> findListBySpec(ClassInfoParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ClassInfoParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ClassInfo findByTeacherId(long teacherId) {
        QueryWrapper<ClassInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", teacherId);
        return this.getOne(queryWrapper);
    }

    private Serializable getKey(ClassInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ClassInfo getOldEntity(ClassInfoParam param) {
        return this.getById(getKey(param));
    }

    private ClassInfo getEntity(ClassInfoParam param) {
        ClassInfo entity = new ClassInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
